package com.bonana.backend.controller;

import com.alibaba.fastjson.JSON;
import com.bonana.backend.services.BackendLoginCheckService;
import com.bonana.backend.session.BackendSession;
import com.bonana.core.result.Response;
import com.bonana.core.services.BoBoService;
import com.bonana.core.vo.SiteStausVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by justin on 15/8/31.
 */
@Controller
public class BackendController {

    @Autowired
    private HttpSession session;

    @Autowired
    private BoBoService boBoService;

    @Autowired
    private BackendLoginCheckService backendLoginCheckService;

    @Autowired
    private HttpServletRequest request;

    @RequestMapping("login")
    public String loginView() {
        return "login";
    }

    @RequestMapping("/logout")
    public String logoout() {
        BackendSession.removeSession(session);
        return "login";
    }

    @RequestMapping("/")
    public String indexView(Model model) {
        return "index";
    }

    @RequestMapping("dashboard")
    public String dashboardView(Model model) {
        return "dashboard";
    }

    @RequestMapping("/validate")
    @ResponseBody
    public Response loginValidate(@RequestParam(required = true) String username, @RequestParam(required = true) String password) {
        return backendLoginCheckService.validate(username, password, session, request.getRemoteAddr());
    }

    @RequestMapping("/getUserActive")
    @ResponseBody
    public List getUserActive() {
        List<SiteStausVo> ssv = boBoService.getActiveUserStatus();
        return ssv;
    }

    @RequestMapping("/getBoBoActive")
    @ResponseBody
    public List getBoBoActive() {
        List<SiteStausVo> ssv = boBoService.getActiveBoBoStatus();
        return ssv;
    }

    @RequestMapping("/getNewUser")
    @ResponseBody
    public List getNewUser() {
        List<SiteStausVo> ssv = boBoService.getNewUsers();
        return ssv;
    }

    @RequestMapping("/getNewComment")
    @ResponseBody
    public List getNewComment() {
        List<SiteStausVo> ssv = boBoService.getNewComments();
        return ssv;
    }
}
